package yunwo.cn.yzstation.api.okapi;

import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Request;
import yunwo.cn.yzstation.base.Yzapp;
import yunwo.cn.yzstation.bean.Bean;
import yunwo.cn.yzstation.bean.YHBBaseBean;
import yunwo.cn.yzstation.utils.CommonUtil;
import yunwo.cn.yzstation.view.activity.LoginActivity;

/**
 * Created by lao on 2017/9/12.
 */

public abstract class GLCallBack extends StringCallback {
    @Override
    public void onResponse(String response, int id) {
        YHBBaseBean bean = new Gson().fromJson(response, new TypeToken<YHBBaseBean>(){}.getType());
        if (bean.getCode() == 401) {
            SharedPreferences.Editor editor = Yzapp.getInstance().getinfoSPEditor();
            editor.clear();
            editor.commit();
            Toast.makeText(CommonUtil.getContext(), "登陆已过期，请重新登陆", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(CommonUtil.getContext(), LoginActivity.class);
            intent.putExtra("logout", "1");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            CommonUtil.getContext().startActivity(intent);
            return;
        }

        String resultStr = new Gson().toJson(bean.getData());
        onResponseT(resultStr,bean.getCode(),bean.getMsg(),id);
    }

    public abstract void onResponseT(String result, int code, String msg, int id);

    @Override
    public void onBefore(Request request, int id) {
        super.onBefore(request, id);
    }

    @Override
    public void onAfter(int id) {
        super.onAfter(id);
    }
}
